/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.wmc.server.container;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.util.Collection;
import org.toools.gwt.wmc.client.container.ContainerService;
import org.toools.gwt.wmc.server.dao.IContainerDAO;
import org.toools.gwt.wmc.jpa.Container;
import org.toools.gwt.wmc.server.WMCSession;

/**
 *
 * @author Manuel
 */
public class ContainerServiceImpl extends RemoteServiceServlet implements ContainerService {

    private IContainerDAO containerdao;

    /** Account DAO **/
    public void setContainerdao( IContainerDAO dao ) {
        this.containerdao = dao;
    }
    public IContainerDAO getContainerdao() {
		return containerdao;
	}


    public Container getContainerByID(Integer ID) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) || containerdao.belongsTo( ID, WMCSession.getAccountID( getThreadLocalRequest() ) ) )
            return containerdao.getContainerByID( ID );

        return null;
    }

    public Collection<Container> getContainers() {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) )
            return containerdao.getContainers();

        return containerdao.getContainers( WMCSession.getAccountID( getThreadLocalRequest() ) );
    }

    public Container create(Container c) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) )
            return containerdao.create( c );
        //check if belongs to account
        else
            return containerdao.create( c );
    }

    public Boolean update(Container c) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) || containerdao.belongsTo( c.getId(), WMCSession.getAccountID( getThreadLocalRequest() ) ) )
            return containerdao.update( c );
        return false;
    }

    public Boolean delete(Container c) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) || containerdao.belongsTo( c.getId(), WMCSession.getAccountID( getThreadLocalRequest() ) ) )
            return containerdao.delete( c );
        return false;
    }
}
